package com.skywork.controller;

import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.skywork.controller.base.BaseController;
import com.skywork.controller.request.CommonDataViewRequest;
import com.skywork.controller.response.CommonDataViewResponse;
import com.skywork.controller.response.base.BaseResponse;
import com.skywork.domain.Channel;
import com.skywork.domain.CommonData;
import com.skywork.service.CommonDataService;
import com.skywork.utils.ChannelKit;
import com.skywork.utils.ValidatorUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@Api(value = "CommonData Service")
@RequestMapping(value = "/common")
public class CommonDataController extends BaseController {

	@Autowired
	private Validator validator;
	@Autowired
	private CommonDataService commonDataService;

	@ApiOperation(value = "通用列表查询", notes = "根据appid查询通用列表")
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
	public BaseResponse<Page<CommonData>> list(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "1") int pageSize) {
		Channel channel = ChannelKit.getChannel();
		LOGGER.info("Channel ID:{}", channel);
		BaseResponse<Page<CommonData>> response = new BaseResponse<Page<CommonData>>();
		Page<CommonData> commonDataList = commonDataService.getList(channel.getId(), pageNum, pageSize);
		response.setData(commonDataList);
		return response;
	}

	@SuppressWarnings(value = "rawtypes")
	@ApiOperation(value = "查询", notes = "根据code查询")
	@RequestMapping(value = "/{code}", method = RequestMethod.POST, produces = "application/json")
	public CommonDataViewResponse view(@PathVariable(value = "code") String code, @ApiParam(required = true, value = "查询对象") @RequestBody CommonDataViewRequest request) {
		CommonDataViewResponse response = new CommonDataViewResponse();
		ValidatorUtils.validateWithException(validator, request);
		Channel channel = ChannelKit.getChannel();
		CommonData obj = new CommonData();
		obj.setChannelId(channel.getId());
		obj.setCode(code);
		CommonData commonData = commonDataService.selectOne(obj);
		if (commonData != null) {
			Page data = commonDataService.queryDynamicSQL(commonData, request);
			LOGGER.info("Page:{}", data.getResult());
			response.setData(data.getResult());
			response.setTotal(data.getTotal());
			response.setPages(data.getPages());
		}
		return response;
	}
}
